﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "EditComponentTypeDlg.h"
#include "ISelection.h"
#include "IDocument.h"
#include "IElementBasicInformation.h"
#include "IUiDocumentViewManager.h"
#include "IMainWindow.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "IFamily.h"
#include "AddNewFamilyTypeDlg.h"
#include "IInstanceType.h"
#include "IDocument.h"
#include "IPropertyWidget.h"
#include "GbmpUiPlatform.h"
#include "IGraphicsNodeReference.h"
#include "ContainerUtil.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

void ShowEditTypeDialogInternal(IUiDocument* pUIDoc, const ElementId& modelId)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMPLab",L"2024-03-30");
    gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();

    ElementId inspectId = modelId;

    EditComponentTypeDlg* pDlg = NEW_AS_QT_CHILD(EditComponentTypeDlg, pDoc, inspectId, dynamic_cast<QWidget*>(IMainWindow::GetMainWindow()));
    if (pDlg->exec() == QDialog::Accepted)
    {
        // 会清除选择集
        pUIDoc->UpdateView(true);

        // UIDocUtil::UpdatePropertyPanel();
    }
}

void CmdShowPropertyDialog(IUiDocument* pUIDoc)
{
    ElementId selectedId;
    if(ISelection::Get()->GetCount() >= 1)
    {
        const GraphicsNodeReferenceOwnerPtrSet& graphicsNodeRefSet = ISelection::Get()->GetGraphicsNodeReferences();
        GraphicsNodeReferenceOwnerPtrSet::const_iterator front = graphicsNodeRefSet.cbegin();
        selectedId = (*front)->GetElementId();
    }
    else
    {
        IUiDocumentViewManager* pUiDocViewMgr = IUiDocumentViewManager::Get();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDocViewMgr, L"pUiDocViewMgr为空",L"GDMPLab",L"2024-03-30");
        selectedId = pUiDocViewMgr->GetCurrentUiView()->GetModelViewId();
    }

    ShowEditTypeDialogInternal(pUIDoc, selectedId);
}

ElementId GetCurrentElementId()
{
    ElementId eleId = ElementId::InvalidID;
    if (ISelection::Get()->GetCount() >= 1)
    {
        const GraphicsNodeReferenceOwnerPtrSet& graphicsNodeRefSet = ISelection::Get()->GetGraphicsNodeReferences();
        GraphicsNodeReferenceOwnerPtrSet::const_iterator front = graphicsNodeRefSet.cbegin();
        eleId = graphicsNodeRefSet.begin()->get()->GetElementId();
    }
    if (eleId.IsValid())
        return eleId;

    std::vector<ElementId> sourceElements;
    //预设参数Element
    IPropertyWidget* pPropertyWidget = IPropertyWidget::GetPropertyWidget();
    if (pPropertyWidget && pPropertyWidget->GetPreSetElementId().IsValid())
    {
        sourceElements.push_back(pPropertyWidget->GetPreSetElementId());
    }

    if (!sourceElements.empty())
    {
        eleId = sourceElements.at(0);
    }

    return eleId;
}

// 显示类型对话框，pUIDoc为空、当前选中element为空没有类型，返回false
bool GBMP_UI_PLATFORM_EXPORT CmdShowTypePropertyDialog(IUiDocument* pUIDoc)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMPLab",L"2024-03-30");

    ElementId selectedId = GetCurrentElementId();
    IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDoc != nullptr, L"pDoc指针无效",L"GDMPLab",L"2024-03-30");

    IElement* pElement = pDoc->GetElement(selectedId);
    if (pElement == nullptr || pElement->GetBasicInformation()->GetType() == nullptr)
    {
        return false;
    }

    ShowEditTypeDialogInternal(pUIDoc, pElement->GetBasicInformation()->GetType()->GetElementId());
    return true;
}

void GBMP_UI_PLATFORM_EXPORT CmdNewFamilyTypeDialog(gcmp::IUiDocument* pUIDoc)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc != nullptr, L"pUIDoc指针无效",L"GDMPLab",L"2024-03-30");

    ElementId selectedId = GetCurrentElementId();
    IDocument* pDoc = pUIDoc->GetDbDocument();

    IElement* pElement = pDoc->GetElement(selectedId);
    if (pElement == nullptr || pElement->GetBasicInformation()->GetType() == nullptr)
        return;

    IType* pInstanceType = pElement->GetBasicInformation()->GetType();

    AddNewFamilyTypeDlg dlg(pDoc, pInstanceType);
    if (dlg.exec() == QDialog::Accepted)
    {
        // UIDocUtil::UpdatePropertyPanel();
    }
}

